<template>
  <div class="app-container">
    <!-- ****************************** -->
    <el-row :gutter="20" class="header" style="margin-left: 0;">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="queryParams.userName"></el-input>
        </el-form-item>
        <el-form-item label="部门">
            <el-cascader v-model="deidss" :options="deptTreeList" :props="props1" clearable
              @change="handleChange1" style="width: 100%;"></el-cascader>
          </el-form-item>
        <el-form-item class="header-button">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
         
        </el-form-item>
      </el-form>

    </el-row>
    <el-table v-loading="loading" :data="menuList" style="width: 100%">
      <el-table-column prop="departmentName" label="用户组织名称" align="center"></el-table-column>
      <el-table-column prop="userName" label="用户名" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="goToDetail(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 尾部分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :visible.sync="dialogVisible" title="编辑部门" width="70%" append-to-body>
      <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="部门" prop="deptId">
            <el-cascader v-model="ruleForm.deptId" :options="deptTreeList" :props="props1" clearable
              @change="handleChange" style="width: 100%;"></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import { getUserPage, treeSelectdept, editUser } from "@/api/system/wechat";
import { downloadExcel } from "@/api/download"

export default {
  name: "Menu",
  dicts: ['sys_show_hide', 'outfield_type', 'outfield_state'],
  components: { Treeselect, IconSelect },
  data () {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('不能为空'));
      }
      if (value.length && value[0]) {
        callback();
      } else {
        callback(new Error('不能为空'));
      }
    };
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: "",
        deptId: "",
      },
      dialogVisible: false,
      selectedI: {},
      Option1: [{
        value: '1',
        label: '内场'
      }, {
        value: '2',
        label: '外场'
      },],
      ruleForm: {
        deptId: undefined,
      },
      rules: {
        deptId: [
          { validator: checkAge, trigger: 'change' },
        ],
      },
      deptTreeList: [],
      props1: { value: 'id', label: 'label', checkStrictly: true },
      deid: 0,
      deidss: 0,
    };
  },
  created () {
    this.getT()
    this.getList();
  },
  activated () {
    this.getT()
    this.getList();
  },
  methods: {
    handleChange (val) {
      if (val && val.length) {
        const a = val.length - 1;
        this.deid = val[a]
      }
    },
    handleChange1 (val) {
      if (val && val.length) {
        const a = val.length - 1;
        this.queryParams.deptId = val[a]
      }else{
        this.queryParams.deptId = ''
      }
    },
    getT () {
      treeSelectdept().then(response => {
        this.deptTreeList = response.data;
      });
    },
    submitForm (formName) {
      console.log(this.ruleForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const p = {
            deptId: this.deid,
            userId: this.selectedI.id,
          }
          editUser(p).then(response => {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.resetForm('ruleForm');
            this.getList();
            this.dialogVisible = false;
          });

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    goToDetail (row) {
      this.dialogVisible = true;
      this.selectedI = row;
    },
    formatter (row, column) {
      return row.sceneType === '1' ? '内场' : '外场'
    },
    /** 查询菜单列表 */
    getList () {
      this.loading = true;
      getUserPage(this.queryParams).then(response => {
        this.total = response.data.total;
        this.menuList = response.data.records;
        this.loading = false;
      });

    },

    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList();

    },

    /** 导出按钮操作 */
    handleExport () {
      if (!this.queryParams.years) {
        this.$message({ message: '请选择年月', type: 'warning' })
        return;
      }
      downloadExcel(`/personnel-performance-management/export?years=${this.queryParams.years}`, `工分_${new Date().getTime()}.xlsx`, {}, 'post')
    },

  }
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;

  .prompt {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 100%;
    background-color: #3f8cda;
    color: white;
    border-radius: 15px;

    h1 {
      text-align: center;
      font-size: 30px;
      letter-spacing: 4px;
    }

    .prompt-main {
      display: flex;
      justify-content: center;

      .vertical-line {
        height: 60px;
        width: 1px;
        background-color: #7b8189;
      }

      .el-col {
        text-align: center;
      }

      .text1 {
        font-size: 30px;
        font-weight: 20px;
        color: #0fec76;
        margin: 5px 0;
      }

      .text2 {
        font-size: 30px;
        font-weight: 20px;
        color: #7a8014;
        margin: 5px 0;
      }

      .text3 {
        font-size: 30px;
        font-weight: 20px;
        color: #bac1d8;
        margin: 5px 0;
      }
    }
  }

  .el-form {
    margin-left: 10px;
    display: flex;
    flex-wrap: wrap;
    flex: 1;

    .el-form-item {
      .el-form-item__label {
        width: 100px;
      }
    }
  }

  .header-button {}
}
</style>
